using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace ICSharpCode.SharpZipLib.Core
{
	public class NameFilter : IScanFilter
	{
		private string filter_;
		private List<Regex> inclusions_;
		private List<Regex> exclusions_;
		public NameFilter(string filter)
		{
			this.filter_ = filter;
			this.inclusions_ = new List<Regex>();
			this.exclusions_ = new List<Regex>();
			this.Compile();
		}
		public static bool IsValidExpression(string expression)
		{
			bool result = true;
			try
			{
				new Regex(expression, 17);
			}
			catch (ArgumentException)
			{
				result = false;
			}
			return result;
		}
		public static bool IsValidFilterExpression(string toTest)
		{
			if (toTest == null)
			{
				throw new ArgumentNullException("toTest");
			}
			bool result = true;
			try
			{
				string[] array = NameFilter.SplitQuoted(toTest);
				for (int i = 0; i < array.Length; i++)
				{
					if (array[i] != null && array[i].get_Length() > 0)
					{
						string text;
						if (array[i].get_Chars(0) == '+')
						{
							text = array[i].Substring(1, array[i].get_Length() - 1);
						}
						else
						{
							if (array[i].get_Chars(0) == '-')
							{
								text = array[i].Substring(1, array[i].get_Length() - 1);
							}
							else
							{
								text = array[i];
							}
						}
						new Regex(text, 17);
					}
				}
			}
			catch (ArgumentException)
			{
				result = false;
			}
			return result;
		}
		public static string[] SplitQuoted(string original)
		{
			char c = '\\';
			char[] array = new char[]
			{
				';'
			};
			List<string> list = new List<string>();
			if (original != null && original.get_Length() > 0)
			{
				int i = -1;
				StringBuilder stringBuilder = new StringBuilder();
				while (i < original.get_Length())
				{
					i++;
					if (i >= original.get_Length())
					{
						list.Add(stringBuilder.ToString());
					}
					else
					{
						if (original.get_Chars(i) == c)
						{
							i++;
							if (i >= original.get_Length())
							{
								throw new ArgumentException("Missing terminating escape character", "original");
							}
							if (Array.IndexOf<char>(array, original.get_Chars(i)) < 0)
							{
								stringBuilder.Append(c);
							}
							stringBuilder.Append(original.get_Chars(i));
						}
						else
						{
							if (Array.IndexOf<char>(array, original.get_Chars(i)) >= 0)
							{
								list.Add(stringBuilder.ToString());
								stringBuilder.set_Length(0);
							}
							else
							{
								stringBuilder.Append(original.get_Chars(i));
							}
						}
					}
				}
			}
			return list.ToArray();
		}
		public override string ToString()
		{
			return this.filter_;
		}
		public bool IsIncluded(string name)
		{
			bool result = false;
			if (this.inclusions_.get_Count() == 0)
			{
				result = true;
			}
			else
			{
				using (List<Regex>.Enumerator enumerator = this.inclusions_.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						Regex current = enumerator.get_Current();
						if (current.IsMatch(name))
						{
							result = true;
							break;
						}
					}
				}
			}
			return result;
		}
		public bool IsExcluded(string name)
		{
			bool result = false;
			using (List<Regex>.Enumerator enumerator = this.exclusions_.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					Regex current = enumerator.get_Current();
					if (current.IsMatch(name))
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}
		public bool IsMatch(string name)
		{
			return this.IsIncluded(name) && !this.IsExcluded(name);
		}
		private void Compile()
		{
			if (this.filter_ != null)
			{
				string[] array = NameFilter.SplitQuoted(this.filter_);
				for (int i = 0; i < array.Length; i++)
				{
					if (array[i] != null && array[i].get_Length() > 0)
					{
						bool flag = array[i].get_Chars(0) != '-';
						string text;
						if (array[i].get_Chars(0) == '+')
						{
							text = array[i].Substring(1, array[i].get_Length() - 1);
						}
						else
						{
							if (array[i].get_Chars(0) == '-')
							{
								text = array[i].Substring(1, array[i].get_Length() - 1);
							}
							else
							{
								text = array[i];
							}
						}
						if (flag)
						{
							this.inclusions_.Add(new Regex(text, 17));
						}
						else
						{
							this.exclusions_.Add(new Regex(text, 17));
						}
					}
				}
			}
		}
	}
}
